package cn.wolfcode.p2p.website.controller;

import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.service.IIpLogService;
import cn.wolfcode.p2p.base.service.ILofinInfoService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.Constants;
import com.sun.xml.internal.ws.wsdl.DispatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.tools.jar.resources.jar_ja;

import javax.servlet.http.HttpSession;

@Controller
public class LgoinInfoCotroller {

    @Autowired
    private ILofinInfoService lofinInfoService;

    @RequestMapping("/userRegister")
    @ResponseBody
    public AjaxResult register(String username, String verifycode, String password, String confirmPwd) {
        //接收参数,进行校验
        AjaxResult result = null;
        //对参数进行福安眼判断
        try {
            lofinInfoService.register(username, verifycode, password, confirmPwd);
            return result = new AjaxResult();
        } catch (DisplayableExcetion e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, "系统出错,请联系管理员");
        }
        return result;
    }

    //查询用户名是否存在
    @RequestMapping("/selectUsername")
    @ResponseBody
    public Object selectUsername(String username) {
        int count = lofinInfoService.selectCountByUsername(username);
        if (count > 0) {
            return new AjaxResult(false, "用户名已存在");
        }
        return new AjaxResult("可以注册");
    }


    //用户登录
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password) {
        AjaxResult result = null;
        try {
            LoginInfo info = lofinInfoService.userLogin(username, password,LoginInfo.USERTYPE_USER);
            if (info == null) {
                throw new DisplayableExcetion("账号或者密码错误");
            }
            return result = new AjaxResult();
        } catch (DisplayableExcetion e) {
            e.printStackTrace();
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(false, "系统出错,请联系管理员");
        }
        return result;

    }



}
